package timetable.student;


import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class main extends TabActivity {
	private static final int ADD_CLASS = 0;
	TabHost tabHost;
	SharedPreferences mPrefs;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        firstRunPreferences();
	   
        Resources res = getResources(); // Resource object to get Drawables
	   tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    tabHost.setBackgroundColor(Color.parseColor("#FF0000"));
	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, TimeTable.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("artists").setIndicator("Timetables",
	                      res.getDrawable(R.drawable.timetableicon))
	                  .setContent(intent);
	    
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, Deadlines.class);
	    spec = tabHost.newTabSpec("albums").setIndicator("Deadlines",
	                      res.getDrawable(R.drawable.deadlinesicon))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    tabHost.setCurrentTab(0);
	    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
    	.setBackgroundColor(Color.parseColor("#C2E1F0"));
	    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.LTGRAY); 
	    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				setTabColors(tabHost);
			}
		});
	    
	    if(getFirstRun())
	    {
	    	showHelpDialog();
	    	setRunned();
	    }
    }
    private void setTabColors(final TabHost tabHost) {
    	for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
    	tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.LTGRAY); 
    	} 

    	tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
    	.setBackgroundColor(Color.parseColor("#C2E1F0"));	
    	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu , menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId())
		{
		case R.id.add:
			startActivityForResult(new Intent(this, AddClass.class), ADD_CLASS);
			return true;
		case R.id.help:
			showHelpDialog();
			return true;
		}
		return false;
	
	}

	private void showHelpDialog()
	{
		LayoutInflater li = LayoutInflater.from(this);
		View view = li.inflate(R.layout.help, null);
		
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    	alertbox.setTitle("Help");
    	
        alertbox.setNeutralButton("Close", new DialogInterface.OnClickListener() {

            // Click listener on the neutral button of alert box
            public void onClick(DialogInterface arg0, int arg1) {

                
            }
        });
        alertbox.setView(view).create().show();
    	TextView text=(TextView) findViewById(R.id.helpText);
	}
	public boolean getFirstRun() {
		return mPrefs.getBoolean("firstRun", true);
		}
		public void setRunned() {

		    SharedPreferences.Editor edit = mPrefs.edit();

		    edit.putBoolean("firstRun", false);

		    edit.commit();

		}
		public void firstRunPreferences() {

		Context mContext = this.getApplicationContext();

		    mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences

		}
}